<g:javascript>
	$(document).ready(function(){
		$("#ti001").autocomplete({
			source: function(request, response) {
		        $.ajax({
		            url: "${createLink(controller:'ajax', action: 'mocti')}",
		            data: { term: request.term },
		            dataType: "json",
		            type: "POST",
		            success: function( data ) {
		            	//data = data.concat(['null', 'null'])
		            	data.unshift(['null', 'null'])
		            	response( $.map( data, function( item ) {
							return {
								  value: item[1]
								, label: item[1]
								, id : item[0]
								, key : item[2]
							}
						}));
		            }
		        });
		    }
			,select: function( event, ui ) {
				$("#ti\\.id").val(ui.item.id);
				$("#ti002").html(ui.item.key);
			}
		});
		
		$.ajax({
			url: "${createLink(controller:'ajax', action: 'workshop')}",
			success: function(data){
				for(var i=0; i<data.length; i++){
					$("#tc020").append("<option value='"+$.trim(data[i])+"'>"+$.trim(data[i])+"</option>");
				}
				$("#tc020").val($.trim("${(tcInstance?.tc020) ? tcInstance?.tc020 : tcInstance?.tb?.tb010}"));
			}
		});
		
	});
</g:javascript>
<g:set var="tb_id" scope="page" >${(tcInstance?.tb?.id==null)?params['tb_id']:tcInstance?.tb?.id}</g:set>
<g:hiddenField name="tb.id" value="${tb_id}" />
<table>
    <tbody>
    
        <tr class="prop">
            <td class="name">
                <label for="tc003"><g:message code="moc.tc.tc003.label" default="Tc003" /></label>
            </td>
            <td class="value ${hasErrors(bean: tcInstance, field: 'tc003', 'errors')}">
                <g:datePicker name="tc003" precision="minute" value="${tcInstance?.tc003}" years="${2009..2015}"  />
            </td>
            <td class="name">
                <label for="tc004"><g:message code="moc.tc.tc004.label" default="Tc004" /></label>
            </td>
            <td class="value ${hasErrors(bean: tcInstance, field: 'tc004', 'errors')}">
                <g:datePicker name="tc004" precision="minute" value="${tcInstance?.tc004}"  years="${2009..2015}" />
            </td>
			<td class="name">
				<label for="tc020"><g:message code="moc.tc.tc020.label" default="Tc020" /></label>
			</td>
			<td class="value">
				<g:select name="tc020" value="${(tcInstance?.tc020) ? tcInstance?.tc020 : tcInstance?.tb?.tb010}"  />
			</td>
        </tr>
        
        <tr class="prop">
            <td class="name">
                <label for="tc001"><g:message code="moc.tc.tc001.label" default="Tc001" /></label>
            </td>
            <td class="value ${hasErrors(bean: tcInstance, field: 'tc001', 'errors')}">
                <g:textField name="tc001" value="${fieldValue(bean: tcInstance, field: 'tc001')}" />
            </td>
            <td class="name">
                <label for="tc002"><g:message code="moc.tc.tc002.label" default="Tc002" /></label>
            </td>
            <td class="value ${hasErrors(bean: tcInstance, field: 'tc002', 'errors')}">
                <g:textField name="tc002" value="${fieldValue(bean: tcInstance, field: 'tc002')}" />
            </td>
            <td class="name">
                <label for="tc022"><g:message code="moc.tc.tc022.label" default="Tc022" /></label> 
            </td>
            <td class="value ${hasErrors(bean: tcInstance, field: 'tc022', 'errors')}">
                <g:textField name="tc022" value="${fieldValue(bean: tcInstance, field: 'tc022')}" />
            </td>
        </tr>
        
        <tr class="prop">
			<td class="name">
      			<label for="ti"><g:message code="moc.tc.ti.label" default="Ti" /></label>
			</td>
			<td class="value ${hasErrors(bean: tcInstance, field: 'ti', 'errors')}">
				<input type="text" id="ti001" name="ti001" value="${tcInstance?.ti?.ti001}"/>
				<g:hiddenField name="ti.id" value="${tcInstance?.ti?.id}" />
				<span id="ti002" style="color:red;"></span>
			</td>             
            <td class="name">&nbsp;</td>
            <td class="value">&nbsp;</td>
            <td class="name">&nbsp;</td>
            <td class="value">&nbsp;</td>
        </tr>
        
    </tbody>
</table>
<table>
    <thead>
        <tr>
            <th><g:message code="moc.tg.td.label" default="Td" /></th>
            <th><g:message code="moc.tg.tg001.label" default="Tg001" /></th>
            <th><g:message code="moc.tg.td.label" default="Td" /></th>
            <th><g:message code="moc.tg.tg001.label" default="Tg001" /></th>
            <th><g:message code="moc.tg.td.label" default="Td" /></th>
            <th><g:message code="moc.tg.tg001.label" default="Tg001" /></th>
        </tr>
    </thead>
    <tbody>
    <g:set var="tgInstanceList" value="${tcInstance.tgs.sort{it.td.td001}}"></g:set>
    <g:set var="tgBlank" value="${(tgInstanceList.size() % 3 == 0)? 0 : 3-tgInstanceList.size() % 3 }"></g:set>
    <g:each in="${tgInstanceList}" status="i" var="tgInstance">
    	<g:if test="${i % 3 == 0}">
    		<g:if test="${i == 0}">
	    		<tr class="${i.intdiv(3) % 2 == 0 ? 'odd' : 'even'}">
    		</g:if>
    		<g:else>
    			</tr>
	    		<tr class="${i.intdiv(3) % 2 == 0 ? 'odd' : 'even'}">
    		</g:else>
            <td><g:hiddenField  name="tgs[${i}].td.id" value="${fieldValue(bean: tgInstance, field: 'td.id')}" /> ${fieldValue(bean: tgInstance, field: "td.td001")}</td>
            <td>
            	<g:textField name="tgs[${i}].tg001" value="${fieldValue(bean: tgInstance, field: 'tg001')}" />
            	<g:hiddenField name="tgs[${i}].tg002" value="${fieldValue(bean: tgInstance, field: 'td.td002')}" />
            </td>
	    	<g:if test="${i ==  tgInstanceList.size() - 1}">
	    		<g:set var="j" value="${0}"></g:set>
	    		<g:while test="${j < tgBlank}">
	    			<g:set var="j" value="${j+1}"></g:set>
	    			<td></td>
	    			<td></td>
	    		</g:while>
				</tr>
	    	</g:if>
    	</g:if>
    	<g:else>
			<td><g:hiddenField  name="tgs[${i}].td.id" value="${fieldValue(bean: tgInstance, field: 'td.id')}" /> ${fieldValue(bean: tgInstance, field: "td.td001")}</td>
			<td>
				<g:textField name="tgs[${i}].tg001" value="${fieldValue(bean: tgInstance, field: 'tg001')}" />
				<g:hiddenField name="tgs[${i}].tg002" value="${fieldValue(bean: tgInstance, field: 'td.td002')}" />
			</td>
	    	<g:if test="${i ==  tgInstanceList.size() - 1}">
	    		<g:set var="j" value="${0}"></g:set>
	    		<g:while test="${j < tgBlank}">
	    			<g:set var="j" value="${j+1}"></g:set>
	    			<td></td>
	    			<td></td>
	    		</g:while>
				</tr>
	    	</g:if>
    	</g:else>
    </g:each>
    </tbody>
</table>
